﻿using DAL;
using FubiManger.UI;
using Model;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Data;

namespace FubiManger.Controls
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class statisticsDelivecontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public statisticsDelivecontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 商户ID
        /// </summary>
        public string merchantid { get; set; }
        List<ms_order> ordlist = new List<ms_order>();
        List<Gdpro> gdlist1 = new List<Gdpro>();
        private void statisticsDelivecontrol_Load(object sender, EventArgs e)
        {
           ordlist = orderDal.QueryDelivery(merchantid);
            
            List<Gdpro> gdlist = new List<Gdpro>();
           
            foreach (var ms_order in ordlist)
            {
                List<Gdpro> gdpro = new List<Gdpro>();
                JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
                gdpro = js.Deserialize<List<Gdpro>>(ms_order.goods_info);
                for (int i = 0; i < gdpro.Count; i++)
                {
                    Gdpro gd = new Gdpro();
                    gd.proname = gdpro[i].proname;
                    gd.count= gdpro[i].count;
                    gdlist.Add(gd);
                }
            }
            
            while (gdlist.Count > 0)
            {
                Gdpro stu = gdlist[0];
                gdlist.RemoveAt(0);//移除元素

                List<Gdpro> list = new List<Gdpro>();//创建临时列表
                foreach (var item in gdlist)
                {
                    if (item.proname.Equals(stu.proname))
                    {
                        stu.count += item.count;
                        stu.proname=item.proname;
                    }
                    else
                    {
                        list.Add(item);
                    }
                }
                gdlist = list;
                gdlist1.Add(stu);
            }
            DataTable table = new DataTable();
            table.Columns.Add("proname");
            table.Columns.Add("count");
            
            for (int i = 0; i < gdlist1.Count; i++)
            {
                table.Rows.Add(gdlist1[i].proname, gdlist1[i].count);
            }
            lvord.DataSource = table;
            lvord.DataBind();
        }

        
        /// <summary>
        /// 出餐
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnout_Press(object sender, EventArgs e)
        {
            if (ordlist.Count > 0)
            {
                if (DateTime.Now > Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 11:00") && DateTime.Now < Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 11:30"))
                {
                    msgcontrol mcon = new msgcontrol();
                    mcon.title = "出餐提示";
                    mcon.content = "您是否已完成所有预定餐的配餐，如果是请点击是";
                    this.Form.ShowDialog(mcon, (obj, args) =>
                    {
                        if (mcon.flag == true)
                        {
                            Delivery delivery = new Delivery();
                            delivery.merchantid = merchantid;
                            string delcount = delivery.SetDelivery();//下单
                            Nmsgcontrol con = new Nmsgcontrol();
                            con.title = "出餐提示";
                            con.content = "本次出餐已完成所有今日预定配送";
                            this.Form.ShowDialog(con);
                            btnout.Enabled = false;
                        }
                    });
                }
                else
                {
                    Nmsgcontrol con = new Nmsgcontrol();
                    con.title = "出餐提示";
                    con.content = "出餐时间为11点至11:30";
                    this.Form.ShowDialog(con);
                }
            }
            else
            {
                Nmsgcontrol con = new Nmsgcontrol();
                con.title = "出餐提示";
                con.content = "当前未有预约订单";
                this.Form.ShowDialog(con);
            }
        }

        private void btnclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}